﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.IO;

namespace EXAM
{
    public partial class QnA : System.Web.UI.Page
    {
        string sErr = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadList();
            }
        }

        private void LoadList()
        {
            BUS.QnAControl ctr = new BUS.QnAControl();
            dlAnswer.DataSource = ctr.GetByType("Q&A", ref sErr);
            dlAnswer.DataBind();
            dlQuest.DataSource = ctr.GetByType("Idea", ref sErr);
            dlQuest.DataBind();
        }

        protected void btnPost_Click(object sender, EventArgs e)
        {
            if (txtQuest.Text != "")
            {
                if (txtQuest.Text != hdOldMsg.Value)
                {
                    BUS.QnAControl ctr = new BUS.QnAControl();
                    DTO.UserInfo user = Session["UserGuest"] as DTO.UserInfo;
                    DTO.QnAInfo inf = new DTO.QnAInfo();
                    inf.IDUserPost = user.ID;
                    inf.Question = txtQuest.Text;
                    inf.Status = "N";
                    inf.Type = "Q&A";
                    if (inf.Type == "Q&A")
                    {
                        //Sendmail(user);
                    }
                    inf.ToEmail = user.ANAL_U3;
                    inf.CreateTime = DateTime.Now;
                    ctr.Add(inf, ref sErr);
                    if (sErr == "")
                    {
                        lbErr.Text = "Send question successful!";
                        hdOldMsg.Value = txtQuest.Text;
                        LoadList();
                    }
                    else lbErr.Text = sErr;
                }
                else lbErr.Text = "you have just sent a spam!";

            }
            else lbErr.Text = "Please input question!";
        }
        private void Sendmail(DTO.UserInfo user)
        {
            //StreamReader sr = new StreamReader(Server.MapPath("templatemail/Feedback.htm"));
            //sr = File.OpenText(Server.MapPath("templatemail/Feedback.htm"));
            string content = txtQuest.Text;

            string email = user.ANAL_U3;// ConfigurationSettings.AppSettings["Email"].ToString();
            BUS.SSRFANVControl analCtr = new BUS.SSRFANVControl();
            DTO.SSRFANVInfo analInf = analCtr.Get("EXA", "U1", user.ANAL_U1, ref sErr);
            try
            {
                Sendmail sm = new Sendmail();
                if (analInf.LOOKUP != "")
                {
                    string[] arrMail = analInf.LOOKUP.Split(';');
                    for (int i = 0; i < arrMail.Length; i++)
                        sm.SendMail("ANI Vietnam e-learning", content, arrMail[i], true, true);
                }

                if (user.Email != "")
                    sm.SendMail("ANI Vietnam e-learning", content, user.Email, true, true);
                //btnReset_Click(null, null);
                lbErr.Text = "Mail sent successfully";
            }
            catch (Exception ex)
            {
                lbErr.Text = ex.Message;
            }
        }
    }
}
